package com.hamny.chat.util;

import java.io.IOException;
import java.nio.charset.Charset;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;

public class HamnyStringHttpMessageConverter extends StringHttpMessageConverter {

	private static MediaType JSON_MEDIA_TYPE = new MediaType("application",
			"json", Charset.forName("utf-8"));

	public HamnyStringHttpMessageConverter() {
		super();
		setWriteAcceptCharset(false);
	}

	@Override
	protected void writeInternal(String json, HttpOutputMessage outputMessage)
			throws IOException {
		System.out.println(json);
		MediaType mediaType = getDefaultContentType(json);
		HttpHeaders headers = outputMessage.getHeaders();
		headers.setContentType(mediaType);
		Long contentLength = getContentLength(json, mediaType);
		headers.setContentLength(contentLength);
		super.writeInternal(json, outputMessage);
	}

	protected MediaType getDefaultContentType(String t) {
		return JSON_MEDIA_TYPE;
	}

}